/////////////////////////////////////////////////////////////////////////////
// Name:        rootdirpage.cpp
// Purpose:     
// Author:      yospaly
// Modified by: 
// Created:     05/01/2010 09:17:03
// RCS-ID:      
// Copyright:   Copyright (C) 2008, yospaly
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include <wx/dir.h>
#include "rootdirpage.h"
#include "mounttable.h"

////@begin XPM images
////@end XPM images


/*!
 * RootdirPage type definition
 */

IMPLEMENT_DYNAMIC_CLASS( RootdirPage, wxWizardPageSimple )


/*!
 * RootdirPage event table definition
 */

BEGIN_EVENT_TABLE( RootdirPage, wxWizardPageSimple )

////@begin RootdirPage event table entries
    EVT_WIZARD_PAGE_CHANGING( -1, RootdirPage::OnRootdirWizardpagePageChanging )

////@end RootdirPage event table entries

END_EVENT_TABLE()


/*!
 * RootdirPage constructors
 */

RootdirPage::RootdirPage()
{
    Init();
}

RootdirPage::RootdirPage( wxWizard* parent )
{
    Init();
    Create( parent );
}


/*!
 * RootdirPage creator
 */

bool RootdirPage::Create( wxWizard* parent )
{
////@begin RootdirPage creation
    wxBitmap wizardBitmap(wxNullBitmap);
    wxWizardPageSimple::Create( parent, NULL, NULL, wizardBitmap );

    CreateControls();
    if (GetSizer())
        GetSizer()->Fit(this);
////@end RootdirPage creation
    return true;
}


/*!
 * RootdirPage destructor
 */

RootdirPage::~RootdirPage()
{
////@begin RootdirPage destruction
////@end RootdirPage destruction
}


/*!
 * Member initialisation
 */

void RootdirPage::Init()
{
////@begin RootdirPage member initialisation
    p_rootdir_ctrl = NULL;
////@end RootdirPage member initialisation
}


/*!
 * Control creation for RootdirPage
 */

void RootdirPage::CreateControls()
{    
////@begin RootdirPage content construction
    RootdirPage* itemWizardPageSimple1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemWizardPageSimple1->SetSizer(itemBoxSizer2);

    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer3, 0, wxGROW, 5);

    wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer3->Add(itemBoxSizer4, 1, wxALIGN_CENTER_VERTICAL, 5);

    wxStaticText* itemStaticText5 = new wxStaticText( itemWizardPageSimple1, wxID_STATIC, _("Select Install Directory"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStaticText5->SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD, false, wxT("Tahoma")));
    itemBoxSizer4->Add(itemStaticText5, 0, wxALIGN_LEFT|wxALL, 5);

    wxStaticText* itemStaticText6 = new wxStaticText( itemWizardPageSimple1, wxID_STATIC, _("Select the directory where you want to install Cygwin. Also choose a few installation parameters."), wxDefaultPosition, wxDefaultSize, 0 );
    itemStaticText6->Wrap(350);
    itemBoxSizer4->Add(itemStaticText6, 0, wxALIGN_LEFT|wxLEFT, 20);

    wxStaticBitmap* itemStaticBitmap7 = new wxStaticBitmap( itemWizardPageSimple1, wxID_STATIC, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer3->Add(itemStaticBitmap7, 0, wxALIGN_CENTER_VERTICAL|wxALL, 20);

    wxStaticLine* itemStaticLine8 = new wxStaticLine( itemWizardPageSimple1, wxID_STATIC, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
    itemBoxSizer2->Add(itemStaticLine8, 0, wxGROW|wxALL, 5);

    wxStaticBox* itemStaticBoxSizer9Static = new wxStaticBox(itemWizardPageSimple1, wxID_ANY, _("Root Directory"));
    wxStaticBoxSizer* itemStaticBoxSizer9 = new wxStaticBoxSizer(itemStaticBoxSizer9Static, wxHORIZONTAL);
    itemBoxSizer2->Add(itemStaticBoxSizer9, 0, wxGROW|wxALL, 5);

    p_rootdir_ctrl = new wxDirPickerCtrl( itemWizardPageSimple1, ID_DIRPICKERCTRL, wxEmptyString, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDIRP_USE_TEXTCTRL|wxDIRP_CHANGE_DIR );
    itemStaticBoxSizer9->Add(p_rootdir_ctrl, 1, wxALIGN_TOP|wxALL, 5);

////@end RootdirPage content construction
}


/*!
 * Should we show tooltips?
 */

bool RootdirPage::ShowToolTips()
{
    return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap RootdirPage::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin RootdirPage bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end RootdirPage bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon RootdirPage::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin RootdirPage icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end RootdirPage icon retrieval
}


/*!
 * wxEVT_WIZARD_PAGE_CHANGING event handler for ID_ROOTDIR_WIZARDPAGE
 */

void RootdirPage::OnRootdirWizardpagePageChanging( wxWizardEvent& event )
{
    const wxString& dir = p_rootdir_ctrl->GetPath();
	if ( dir.IsEmpty() ) {
        wxMessageBox(_("You MUST select a directory before installation."), _("Error"), wxOK);
        event.Veto();
        return;
	}

    wxDir wxdir(dir);

    if ( wxdir.HasFiles() || wxdir.HasSubDirs() ) {
        wxMessageBox(wxString::Format(_("Directory \"%s\" is not empty"), dir.c_str()), _("Error"), wxOK);
        event.Veto();
        return;
    }

    /* if ( !wxDir::Exists(dir) ) {
        wxMessageBox(wxString::Format(_("Directory \"%s\" doesn't exists"), dir.c_str()), _("Error"), wxOK);
        event.Veto();
        return;
    } */

    // Set Cygwin root (install) dir
    MountTable& mt = MountTable::get();
    mt.set_root_dir(dir);

    /*
    // Mark all Base packages installed, then send "Apply" message
    IniBuilder::get().mark_all_base_pkgs_installed();

    // apply
    wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, ID_APPLY_TOOL);
    evt.SetEventObject(this);
    ::wxPostEvent(this, evt);
    */
}

